﻿using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

namespace NUnitTest
{
    [TestFixture]
    public class TestCaseDemo
    {
        private IWebDriver driver;
        private StringBuilder verificationErrors;
        private string baseURL;
        private bool acceptNextAlert = true;
        private string browserName;
        public string BrowserName { get => browserName; set => browserName = value; }

        [SetUp]
        public void SetupTest()
        {
            //browserName = "edge";
            switch (BrowserName)
            {
                case "ie"://打开IE浏览器(确保已安装IE)，会调用\bin\Debug下的IEDriverServer.exe。  
                    driver = new InternetExplorerDriver();
                    break;
                case "chrome"://打开Chrome浏览器(确保已安装Chrome)，会调用\bin\Debug下的chromedriver.exe。  
                    driver = new ChromeDriver();
                    break;
                case "firefox"://打开FireFox浏览器(确保已安装FireFox)，会调用\bin\Debug下的Ionic.Zip.dll。  
                    driver = new FirefoxDriver();
                    break;
                case "edge":
                    driver = new EdgeDriver();
                    break;
                default:
                    driver = new ChromeDriver();
                    break;
            }
            //driver = new ChromeDriver();
            baseURL = "https://www.katalon.com/";
            verificationErrors = new StringBuilder();
        }

        [TearDown]
        public void TeardownTest()
        {
            try
            {
                //driver.Quit();
            }
            catch (Exception)
            {
                // Ignore errors if unable to close the browser
            }
            Assert.AreEqual("", verificationErrors.ToString());
        }
        [Test]
        public void TestCaseDemoTest()
        {
            driver.Navigate().GoToUrl("https://www.baidu.com/");
            driver.FindElement(By.Id("kw")).Click();
            driver.FindElement(By.Id("kw")).Clear();
            driver.FindElement(By.Id("kw")).SendKeys("你好啊");
            driver.FindElement(By.Id("form")).Submit();

            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));

            wait.Until(ExpectedConditions.ElementExists(By.Id("container")));
            driver.FindElement(By.Id("container")).Click();
            driver.FindElement(By.LinkText("下一页>")).Click();

            WebDriverWait wait1 = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
            wait1.Until(ExpectedConditions.ElementExists(By.XPath("//div[@id='14']/h3/a/em")));

            driver.FindElement(By.XPath("//div[@id='14']/h3/a/em")).Click();
            // ERROR: Caught exception [ERROR: Unsupported command [selectWindow | win_ser_1 | ]]
        }
        [Test]
        public void LongForTestCase()
        {
            //driver.Navigate().GoToUrl("http://10.229.16.18/longhui/");
            driver.Navigate().GoToUrl("http://w-xiefeng:a-1@@XIEFENG@10.229.16.18/longhui/");
            driver.Manage().Window.Maximize();
            //隐式等待，所有找元素的方法都默认等待3秒钟内去找
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(3);
            driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(30);
            driver.Manage().Timeouts().AsynchronousJavaScript= TimeSpan.FromSeconds(30);
            driver.SwitchTo().Frame("InlineDialog_Iframe");
            var buttonClose = By.Id("buttonClose");
            driver.FindElement(buttonClose).Click();//关闭弹框
            //driver.FindElement(By.ClassName("navTabButtonArrowDown")).Click();//点击下拉框切换到菜单
            //跳转到新建线索
            driver.Navigate().GoToUrl("http://10.229.16.18/longhui/main.aspx?etc=4&extraqs=&histKey=369127351&newWindow=true&pagetype=entityrecord#474213773");
            driver.SwitchTo().Frame("contentIFrame0");
            //执行js脚本
            IJavaScriptExecutor jse = (IJavaScriptExecutor)driver;
            jse.ExecuteScript("Xrm.Page.getAttribute(\"subject\").setValue(\"2020年6月12日test\")");
            jse.ExecuteScript("Xrm.Page.getAttribute('fullname').setValue('1234')");
            //IAlert alert = driver.SwitchTo().Alert();   //转到弹出框
            ////确定：Alert , Confirm, Prompt
            ////alert.Dismiss();                            //取消：Confirm, Prompt
            //var text = alert.Text;                      //获取提示内容：Alert , Confirm, Prompt
            //Console.WriteLine(text);
            //alert.SetAuthenticationCredentials("15116373824","a-1@@XIEFENG");              //输入提示文本：Prompt
            //alert.Accept();
        }
        private bool IsElementPresent(By by)
        {
            try
            {
                driver.FindElement(by);
                return true;
            }
            catch (NoSuchElementException)
            {
                return false;
            }
        }

        private bool IsAlertPresent()
        {
            try
            {
                driver.SwitchTo().Alert();
                return true;
            }
            catch (NoAlertPresentException)
            {
                return false;
            }
        }

        private string CloseAlertAndGetItsText()
        {
            try
            {
                IAlert alert = driver.SwitchTo().Alert();
                string alertText = alert.Text;
                if (acceptNextAlert)
                {
                    alert.Accept();
                }
                else
                {
                    alert.Dismiss();
                }
                return alertText;
            }
            finally
            {
                acceptNextAlert = true;
            }
        }
    }
}


